package com.smartleanx.module.hrm.convert.employee.contract;

import cn.hutool.core.collection.CollUtil;
import com.smartleanx.framework.common.enums.CommonStatusEnum;
import com.smartleanx.framework.common.util.collection.CollectionUtils;
import com.smartleanx.framework.common.util.collection.MapUtils;
import com.smartleanx.framework.common.util.object.BeanUtils;
import com.smartleanx.module.hrm.controller.admin.employee.login.EmployeeLoginInfoPageReqVO;
import com.smartleanx.module.hrm.controller.admin.employee.login.EmployeeLoginInfoSaveReqVO;
import com.smartleanx.module.hrm.controller.admin.employee.login.EmployeeLoginInfoRespVO;
import com.smartleanx.module.hrm.controller.admin.employee.vo.EmployeeSimpleRespVO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.eums.employee.EmployeeStatusEnum;
import com.smartleanx.module.system.api.dept.dto.DeptRespDTO;
import com.smartleanx.module.system.api.user.dto.AdminUserPageReqDTO;
import com.smartleanx.module.system.api.user.dto.AdminUserRespDTO;
import com.smartleanx.module.system.api.user.dto.AdminUserSaveReqDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 员工
 *
 * @author tengcj
 * @since 2024/7/1
 */
@Mapper
public interface EmployeeConvert {

    EmployeeConvert INSTANCE = Mappers.getMapper(EmployeeConvert.class);

    default AdminUserSaveReqDTO convertAdminUserSaveReqDTO(EmployeeLoginInfoSaveReqVO reqVO) {
        AdminUserSaveReqDTO saveReqDTO = new AdminUserSaveReqDTO();
        saveReqDTO.setUsername(reqVO.getUsername());
        saveReqDTO.setPassword(reqVO.getPassword());
        saveReqDTO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        saveReqDTO.setNickname(reqVO.getNickname());
        saveReqDTO.setMobile(reqVO.getMobile());
        saveReqDTO.setSex(reqVO.getSex());
        saveReqDTO.setEmail(reqVO.getPersonalEmail());
        saveReqDTO.setRemark(reqVO.getRemark());
        saveReqDTO.setDeptId(reqVO.getDeptId());
        if (reqVO.getPostId() != null) {
            saveReqDTO.setPostIds(CollUtil.newHashSet(reqVO.getPostId()));
        }
        return saveReqDTO;
    }

    AdminUserSaveReqDTO convertAdminUserSaveReqDTO(AdminUserRespDTO adminUser);

    default AdminUserSaveReqDTO convertAdminUserSaveReqDTO(EmployeeDO employeeDO, EmployeeLoginInfoSaveReqVO reqVO) {
        AdminUserSaveReqDTO saveReqDTO = new AdminUserSaveReqDTO();
        saveReqDTO.setUsername(reqVO.getUsername());
        saveReqDTO.setPassword(reqVO.getPassword());
        saveReqDTO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        saveReqDTO.setNickname(employeeDO.getName());
        saveReqDTO.setMobile(employeeDO.getMobile());
        saveReqDTO.setSex(employeeDO.getSex());
        saveReqDTO.setEmail(employeeDO.getPersonalEmail());
        saveReqDTO.setRemark(reqVO.getRemark());
        saveReqDTO.setDeptId(employeeDO.getDeptId());
        if (reqVO.getRoleId() != null){
             saveReqDTO.setRoleIds(CollUtil.newHashSet(reqVO.getRoleId()));
        }
        if (employeeDO.getPostId() != null) {
            saveReqDTO.setPostIds(CollUtil.newHashSet(employeeDO.getPostId()));
        }
        return saveReqDTO;
    }

    default EmployeeLoginInfoRespVO convertEmployeeLoginRespVO(AdminUserRespDTO adminUser) {
        if (adminUser == null) {
            return null;
        }

        EmployeeLoginInfoRespVO employeeLoginInfoRespVO = new EmployeeLoginInfoRespVO();

        employeeLoginInfoRespVO.setId(adminUser.getId());
        employeeLoginInfoRespVO.setUsername(adminUser.getUsername());
        employeeLoginInfoRespVO.setNickname(adminUser.getNickname());
        employeeLoginInfoRespVO.setDeptId(adminUser.getDeptId());
        employeeLoginInfoRespVO.setPersonalEmail(adminUser.getEmail());
        employeeLoginInfoRespVO.setMobile(adminUser.getMobile());
        employeeLoginInfoRespVO.setSex(adminUser.getSex());
        employeeLoginInfoRespVO.setAvatar(adminUser.getAvatar());
        employeeLoginInfoRespVO.setStatus(adminUser.getStatus());
        employeeLoginInfoRespVO.setRemark(adminUser.getRemark());
        employeeLoginInfoRespVO.setPostId(CollUtil.getFirst(adminUser.getPostIds()));
        employeeLoginInfoRespVO.setCreateTime(adminUser.getCreateTime());

        return employeeLoginInfoRespVO;
    }

    EmployeeSimpleRespVO convertSimpleRespVO(EmployeeDO employeeDO);

    default List<EmployeeSimpleRespVO> convertSimpleList(List<EmployeeDO> list, Map<Long, DeptRespDTO> deptMap) {
        return CollectionUtils.convertList(list, employeeDO -> {
            EmployeeSimpleRespVO respVO = convertSimpleRespVO(employeeDO);
            MapUtils.findAndThen(deptMap, respVO.getDeptId(), dept -> respVO.setDeptName(dept.getName()));
            return respVO;
        });
    }

    default List<EmployeeLoginInfoRespVO> convertLoginInfoList(List<AdminUserRespDTO> list,
                                                      Map<Long, DeptRespDTO> deptMap,
                                                      Map<Long, EmployeeDO> employeeLoginInfoMap) {
        List<EmployeeLoginInfoRespVO> result = new ArrayList<>();
        if (CollUtil.isEmpty(list)) {
            return result;
        }
        for (AdminUserRespDTO adminUserRespDTO : list) {
            EmployeeLoginInfoRespVO loginInfoRespVO = convertEmployeeLoginRespVO(adminUserRespDTO);
            MapUtils.findAndThen(deptMap, loginInfoRespVO.getDeptId(), dept -> loginInfoRespVO.setDeptName(dept.getName()));
            MapUtils.findAndThen(employeeLoginInfoMap, loginInfoRespVO.getId(), emp -> {
                loginInfoRespVO.setEmployeeId(emp.getId());
                loginInfoRespVO.setEmployeeName(emp.getName());
                loginInfoRespVO.setState(EmployeeStatusEnum.getName(emp.getState()));
            });
            result.add(loginInfoRespVO);
        }
        return result;
    }

    AdminUserPageReqDTO convertLoginInfoPageReqDTO(EmployeeLoginInfoPageReqVO pageReqVO);
}
